package com.du.dumall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.du.dumall.product.dao.BrandDao;
import com.du.dumall.product.dao.CategoryDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.du.dumall.common.utils.PageUtils;
import com.du.dumall.common.utils.Query;

import com.du.dumall.product.dao.CategoryBrandRelationDao;
import com.du.dumall.product.entity.CategoryBrandRelationEntity;
import com.du.dumall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id",params.get("brandId"));
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelation.setBrandName(brandDao.selectById(categoryBrandRelation.getBrandId()).getName());
        categoryBrandRelation.setCatelogName(categoryDao.selectById(categoryBrandRelation.getCatelogId()).getName());
        this.save(categoryBrandRelation);

    }

    @Override
    public void updateName(Long brandId, String name) {
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        baseMapper.update(relationEntity,wrapper.eq("brand_id",brandId));
    }

    @Override
    @Transactional
    public void updateCascade(Long catId, String name) {
        this.baseMapper.updateCascade(catId,name);
    }

    @Override
    public List<CategoryBrandRelationEntity> brandsList(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        return relationEntities;
    }

}